<%@ page language="java" contentType="text/html;charset=UTF-8"
	pageEncoding="utf-8"%>
<html>
<head>
	<meta charset="UTF-8">
	<title>mamadi</title>
	<link rel="stylesheet" type="text/css" href="css/common.css">
</head>
<body>
	<div class="wrapper">
		<div class="header">
			<div class="heading">
				<div class="logo">
					Hi,I am logo.<br/>
					嗨,我是麻麻的。
				</div>
				<div class="sertch-box">
					<input type="text" placeholder="输入关键字" />
					<span class="btn search-btn">搜索</span>
				</div>
			</div>
		</div>
		<div class="content">
			<div class="box">
				<div class="row">	
					<div class="attr">订单号：</div>
					<div class="data" id="order-num"></div>
				</div>
				<div class="row">
				
					<div class="attr">客户号：</div>
					<div class="data"><input id="client-id" type="text" placeholder="编号或者手机号码" onpropertychange="isNum(this)" onblur="searchClient(this);" oninput="isNum(this)"></div>
				</div>
				<div class="box" id="goods-box">
					<div class="row">
						<div class="row">
							<div class="attr">编&nbsp;号：</div>
							<div class="data"><input class="goods-name" type="text" placeholder="商品编号" onblur="searchGoodsName(this)"></div>
						</div>
						<div class="row">
							<div class="attr">商品名：</div>
							<div class="data goodName"></div>
						</div>
						<div class="row">
							<div class="attr">价&nbsp;格：</div>
							<div class="data price"></div>
						</div>
						<div class="row count">
							<div class="attr">数&nbsp;量：</div>
							<div class="data">
								<span class="btn dec">﹣</span>
								<input class="count-num" type="text" value="0" onpropertychange="isNum(this);isNull(this);sumUp();largeThanStorage(this)" oninput="isNum(this);isNull(this);sumUp();largeThanStorage(this)" onblur="isNum(this);isNull(this);sumUp();largeThanStorage(this)" onchange="isNum(this);isNull(this)" />
								<span class="btn add">﹢</span>
							</div>
						</div>
					</div>
				</div>
					
				<div class="row">
					<div class="btns" onclick="addItem(this)">增加新购买项</div>
				</div>
				<div class="row">
					<div class="attr">总&nbsp;计：</div>
					<div class="data">￥<span id="sum">0.00</span></div>
				</div>
				<div class="row">
					<div class="btn" onclick="submit();">提交</div>
				</div>
			</div>
		</div>
		<div class="footer"></div>
	</div>
</body>
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="js/common.js"></script>
<script type="text/javascript">
	var orderArray = new Array();
	var productArrays = new Array();
	var orderLength = 0;
	if(localStorage.getItem("order") != null) {
			orderArray = JSON.parse(localStorage.getItem("order"));
			orderLength = orderArray.length;
	}
	$(document).ready(function () {
		createOrderNum();
	});

	function addItem(obj) {
		var s = "";
			s += '<div class="row">';
			s += '<div class="row">';
			s += '<div class="attr">编&nbsp;号：</div>';
			s += '<div class="data"><input class="goods-name" type="text" placeholder="商品编号" onblur="searchGoodsName(this)"></div>';
			s += '</div>';
			s += '<div class="row">';
			s += '<div class="attr">商品名：</div>';
			s += '<div class="data goodName"></div>';
			s += '</div>';
			s += '<div class="row">';
			s += '<div class="attr">价&nbsp;格：</div>';
			s += '<div class="data price"></div>';
			s += '</div>';
			s += '<div class="row count">';
			s += '<div class="attr">数&nbsp;量：</div>';
			s += '<div class="data">';
			s += '<span class="btn dec">﹣</span>';
			s += '<input class="count-num" type="text" value="0" onpropertychange="isNum(this);isNull(this);sumUp();largeThanStorage(this)" oninput="isNum(this);isNull(this);sumUp();largeThanStorage(this)" onblur="isNum(this);isNull(this);sumUp();largeThanStorage(this)" onchange="isNum(this);isNull(this)" />';
			s += '<span class="btn add">﹢</span>';
			s += '</div>';
			s += '</div>';
			s += '</div>';
		$(obj).parent(".row").prev(".box").append(s);
	}

	function createOrderNum () {
		var date = new Date();
		var year = date.getFullYear();
		var month = ifLessThanTen(date.getMonth() + 1);
		var date = ifLessThanTen(date.getDate());
		$("#order-num").text("so-" + year + month + date + "-00" + (orderLength + 1));
	}

	function ifLessThanTen(num) {
		if(parseInt(num) < 10) {
			var str = num + "";
				str = "0" + num;
			return str;
		}
		else {
			return num;
		}
	}

	function searchGoodsName(obj) {
		var flag = 0;
		var val = $(obj).val();
		var productArray = new Array();
		if(localStorage.getItem("product") != null) {
			productArrays = JSON.parse(localStorage.getItem("product"));
			productArray = JSON.parse(localStorage.getItem("product"));
			for(var i = 0; i < productArray.length; i++) {
				if(productArray[i].id == $(obj).val()) {
					$(obj).parents(".row").nextAll(".row").children(".goodName").text(productArray[i].attr1);
					$(obj).parents(".row").nextAll(".row").children(".price").text(productArray[i].attr2);
					var s = "";
						s += '<div class="data">';
						s += '（<span>库存：</span>';
						s += '<span class="storage-num">';
						s += productArray[i].attr3;
						s += '</span>）';
						s += '</div>';
					$(obj).parents(".row").nextAll(".count").append(s);
					flag = 1;
					break;
				}
				else {
					flag = 0;
				}
			}
			var sum = parseFloat($("#sum").text());
			var price = parseFloat($(obj).parents(".row").nextAll(".row").children(".price").text());
			var count = parseFloat($(obj).parents(".row").nextAll(".row").find(".count-num").val());
			if(val == "") {
				alert("请输入商品编号");
				$(obj).parents(".row").nextAll(".row").find(".storage-num").parents(".data").remove();
				$(obj).parents(".row").nextAll(".row").find(".count-num").val("0");
				$(obj).parents(".row").nextAll(".row").children(".price").text("");
				$(obj).parents(".row").nextAll(".row").children(".goodName").text("");
				$("#sum").text((sum - price * count).toFixed(2));
			}
			else if(flag == 0) {
				alert("查无此商品，请确认！");
				$(obj).parents(".row").nextAll(".row").find(".storage-num").parents(".data").remove();
				$(obj).parents(".row").nextAll(".row").find(".count-num").val("0");
				$(obj).parents(".row").nextAll(".row").children(".price").text("");
				$(obj).parents(".row").nextAll(".row").children(".goodName").text("");
				$("#sum").text((sum - price * count).toFixed(2));
			}
		}
	}

	function searchClient(obj) {
		var flag = 0;
		var val = $(obj).val();
		var clientArray = new Array();
		if(localStorage.getItem("client") != null) {
			clientArray = JSON.parse(localStorage.getItem("client"));
			for(var i = 0; i < clientArray.length; i++) {
				if(clientArray[i].id != val && clientArray[i].attr4 != val) {
					alert("用户不存在，请重新输入");
					$(obj).val("");
					break;
				}
			}
		}
	}

	function sumUp() {
		var sum = 0;
		for(var i = 0; i < $("#goods-box").children(".row").size(); i++) {
			if($("#goods-box").children(".row").eq(i).find(".price").text() != "") {
				var price = parseFloat($("#goods-box").children(".row").eq(i).find(".price").text());
				var count = parseFloat($("#goods-box").children(".row").eq(i).find(".count-num").val());
				sum += price * count;
			}
		}
		$("#sum").text(sum.toFixed(2));
	}

	$(document).on("click", ".add",function() {
		var val = parseInt($(this).parent(".data").children(".count-num").val());
		if($(this).parent(".data").next().children(".storage-num").text() == "") {
			return ;
		}
		val >= parseInt($(this).parent(".data").next().children(".storage-num").text()) ? val : $(this).parent(".data").children(".count-num").val(++val);

		sumUp();
	});

	$(document).on("click", ".dec",function() {
		var val = parseInt($(this).parent(".data").children(".count-num").val()); 
		val <= 0 ? val : $(this).parent(".data").children(".count-num").val(--val);

		sumUp();
	});

	function largeThanStorage(obj) {
		var storage = 0;
		var val = parseInt($(obj).val());
		if($(obj).parents(".row").find(".goodName").text() != "") {
			for(var i = 0; i < productArrays.length; i++) {
				if(productArrays[i].attr1 == $(obj).parents(".row").find(".goodName").text()) {
					storage = parseInt(productArrays[i].attr3);
				}
			}
			if(val > storage) {
				alert("库存不足，请重新输入");
				$(obj).val("0");

				sumUp();
			}
		}
		else {
			if(val > storage) {
				alert("都不知道你要买什么，别动！");
				$(obj).val("0");

				sumUp();
			}
		}
	}

	function submit() {
		if($("#client-id").val() == "") {
			alert("请输入客户手机或者客户编号");
		}
		else if($("#sum").text() == "0.00") {
			alert("请选择购买项");
		}
		else {
			var orderId = $("#order-num").text();
			var clientId = $("#client-id").val();
			var buyItemArray = new Array();
			for(var i = 0; i < $("#goods-box").children(".row").size(); i++) {
				var param = {};
				for(var j = 0; j < 2; j++) {
					param["goodName"] = $("#goods-box").children(".row").eq(i).find(".goodName").text();
					param["countNum"] = $("#goods-box").children(".row").eq(i).find(".count-num").val();
				}
				buyItemArray.push(param);
			}
			var sum = $("#sum").text();

			var orderList = {};
			orderList["orderId"] = orderId;
			orderList["clientId"] = clientId;
			orderList["sum"] = sum;
			orderList["buyItemArray"] = buyItemArray;
			orderList["status"] = 0;

			orderArray.push(orderList);

			console.log(orderArray);
			localStorage.setItem("order", JSON.stringify(orderArray));
			window.location.reload();
		}
	}
</script>
</html>